﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Question2
{
    class Program
    {
        static void Main(string[] args)
        {
            Clock clock = new Clock();
            clock.AlarmTime= new Time(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second+3);
            clock.TickEvent += clock.ShowTime;
            clock.AlarmEvent+=clock.Alarm;
            clock.Run();
        }
    }

    public class Clock
    {
        public Clock() { }
        public event Action AlarmEvent;
        public event Action TickEvent;
        public Time AlarmTime { get; set; }
        public Boolean IsAlarmTime(Time t)
        {
            if (t.Hour == AlarmTime.Hour &&
                t.Minute == AlarmTime.Minute &&
                t.Second == AlarmTime.Second)
                return true;
            else
                return false;
        }
        public void ShowTime()
        {
            Console.WriteLine(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second);
        }
        public void Alarm()
        {
            Console.WriteLine("Alarm.");
        }

        public void Run()
        {
            while (true)
            {
                TickEvent();
                if (IsAlarmTime(new Time(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second)))
                    AlarmEvent();
                System.Threading.Thread.Sleep(1000);
            }
        }
    }

    public class Time
    {
        private int hour;
        private int minute;
        private int second;
        public Time(int hour , int minute , int second )
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }

        public int Hour
        {
            get
            { return hour; }
            set
            {
                if (value < 0 || value > 24)
                    throw new Exception("Wrong Hour");
                else
                    hour = value;
            }
        }

        public int Minute
        {
            get
            { return minute; }
            set
            {
                if (value < 0 || value > 60)
                    throw new Exception("Wrong Minute");
                else
                    minute = value;
            }
        }

        public int Second
        {
            get
            { return second; }
            set
            {
                if (value < 0 || value > 60)
                    throw new Exception("Wrong Minute");
                else
                    second = value;
            }
        }
    }
}
